<template>
  <div class='Contetn'>
    <div class="category">
      <div :class="{'category_item':true,'category_item--active':currentTab === item.tab}"
        v-for="item in categories" :key="item.name"
        @click="() => handleTabClick(item.tab)">
        {{item.name}}
      </div>
    </div>
    <div class="product">
      <div class="product_item" v-for="(item,_id) in list" :key="_id">
        <img class="product_item_img" :src="item.imgUrl" alt="">
        <div class="product_item_detail">
          <h4 class="product_item_title">{{item.name}}</h4>
          <p class="product_item_sales">{{item.sales}}</p>
          <p class="product_item_price">
            <span class="product_item_yen">&yen;</span>{{item.price}}
            <span class="product_item_origin">&yen;{{item.oldPrice}}</span>
          </p>
        </div>
        <div class="product_number">
          <span
            class="product_number_minus iconfont"
            @click="()=>{changeCartItem(shopId,item._id,item,-1,shopName)}">&#xe677;</span>
            {{cartList?.[shopId]?.productList?.[item._id]?.count || 0}}
            <!-- {{item.count||0}} -->
          <span
            class="product_number_plus iconfont"
            @click="()=>{changeCartItem(shopId,item._id,item,1,shopName)}">&#xe845;</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, ref, toRefs, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { get } from '@/utils/request'
import { useCommonCartEffect } from '@/effects/cartEffects'
const categories = [
  { name: '全部商品', tab: 'all' },
  { name: '秒杀', tab: 'seckill' },
  { name: '新鲜水果', tab: 'fruit' }
]
// Tab切换相关的逻辑
const useTabEffect = () => {
  const currentTab = ref(categories[0].tab)
  const handleTabClick = (tab) => {
    currentTab.value = tab
  }
  return { handleTabClick, currentTab }
}

// 列表内容相关的数据
const useCurrentListEffect = (currentTab, shopId) => {
  const content = reactive({ list: [] })
  const getContentData = async (tab) => {
    const result = await get(`/api/shop/${shopId}}/products`, { tab: currentTab.value })
    if (result?.errno === 0 && result?.data?.length) {
      content.list = result.data
    }
  }
  watchEffect(() => { getContentData() })
  const { list } = toRefs(content)
  return { list }
}

const useCartEffect = () => {
  const store = useStore()
  const { cartList, changeCartItemInfo } = useCommonCartEffect()
  const changeShopName = (shopId, shopName) => {
    store.commit('changeShopName', {
      shopId, shopName
    })
  }
  const changeCartItem = (shopId, productId, item, num, shopName) => {
    changeCartItemInfo(shopId, productId, item, num)
    changeShopName(shopId, shopName)
  }
  return { cartList, changeCartItem }
}
export default {
  name: 'ShopContetn',
  props: ['shopName'],
  components: {},
  // 业务逻辑
  setup () {
    const route = useRoute()
    const shopId = route.params.id
    const { handleTabClick, currentTab } = useTabEffect()
    const { list } = useCurrentListEffect(currentTab, shopId)
    const { cartList, changeCartItem } = useCartEffect()
    return { changeCartItem, list, categories, handleTabClick, currentTab, shopId, cartList }
  }
}
</script>
<style lang='scss' scoped>
@import '@/style/viriables.scss';
@import '@/style/mixins.scss';
.Contetn{
  display: flex;
  position: absolute;
  left: 0;
  right: 0;
  top: 1.5rem;
  bottom: .5rem;
}
.category{
  width: .76rem;
  height: 100%;
  overflow-y: scroll;
  background: $search-bgColor;
  &_item{
    line-height: .4rem;
    text-align: center;
    font-size: .14rem;
    color: $content-fontcolor;
    &--active{
      background: $bg-color;
    }
  }
}
.product{
  flex: 1;
  overflow-y: scroll;
  &_item{
    position: relative;
    display: flex;
    padding: .12rem 0;
    margin: 0 .16rem;
    border-bottom: .01rem solid $content-bgColor;
    &_detail{
      overflow: hidden;
    }
    &_img{
      width: .68rem;
      height: .68rem;
      margin-right: .16rem;
    }
    &_title{
      margin: 0;
      line-height: .2rem;
      font-size: .14rem;
      color: $content-fontcolor;
      @include ellipse;
    }
    &_sales{
      margin: .06rem 0;
      line-height: .16rem;
      font-size: .12rem;
      color: $content-fontcolor;
    }
    &_price{
      margin: 0;
      line-height: .2rem;
      font-size: .14rem;
      color: $highlight-fontColor;
    }
    &_yen{
      font-size: .12rem;
    }
    &_origin{
      margin-left: .06rem;
      line-height: .2rem;
      font-size: .12rem;
      color: $light-fontColor;
      text-decoration: line-through;
    }
    .product_number{
      position:absolute;
      right: 0;
      bottom: .12rem;
      line-height: .18rem;
      &_minus{
        color: $medium-fontColor;
        margin-right: .02rem;
        position: relative;
        top: .01rem;
      }
      &_plus{
        position: relative;
        top: .01rem;
        color: #0091ff;
        margin-left: .06rem;
      }
    }
  }
}
</style>
